
package system.base;

import framework.core.*;
import system.data.*;
/*
	Author    :chengbiao.he
	CreateTime:2008-02-22 15:08:30
*/
public class WarehouseProductSrv extends IHandler
{

	public WarehouseProductSrv() 
	{
		super();
	}
	private void deleteWarehouseProduct(DBConnection con,SrvContext context) throws Exception
	{
		String[] ids = context.getParameterValues("ids");
		WarehouseProduct obj = null;
		for(int i=0;i<ids.length;i++)
		{
			obj = WarehouseProduct.fetch(con,ids[i]);
			if(obj!=null) obj.delete(con);
		}
	}
	private void saveWarehouseProduct(DBConnection con,SrvContext context,ILoginContext loginContext) throws Exception
	{
		String id = context.getString("Id",null);
		WarehouseProduct obj;
		if((id==null)||(id.equals("")))   //add
		{
			obj = WarehouseProduct.fetch();
    	}
    	else                     //update
    	{
    		obj = WarehouseProduct.fetch(con,id);
    	}
		WarehouseProductPageData data = (WarehouseProductPageData)PageData.fromForm(context,WarehouseProductPageData.class);
		context.setValue("WarehouseProductPageData",data);
		obj.fill(data);
		obj.save(con);
	}
	private void listWarehouseProduct(DBConnection con,SrvContext context,String path) throws Exception
	{
		WarehouseProductSearchEvent event = (WarehouseProductSearchEvent)SearchEvent.fromForm(context,WarehouseProductSearchEvent.class);
		context.setSessionValue("WarehouseProductSearchEvent",event);
		context.setNextUrl(path+"WarehouseProductList.jsp");
	}
	private void backWarehouseProduct(DBConnection con,SrvContext context,String path) throws Exception
	{
		context.setNextUrl(path+"WarehouseProductList.jsp");
	}
	
	public void doAction(DBConnection con,SrvContext context,ILoginContext loginContext) throws Exception
	{
		String action_code = context.getActionCode();
		String path = "/system/base/";
		
		if(action_code.equalsIgnoreCase("listWarehouseProduct"))
		{
			listWarehouseProduct(con,context,path);
			return;
		}
		if(action_code.equalsIgnoreCase("viewWarehouseProduct"))
		{
			String id = context.getString("Id",null);
			WarehouseProduct obj = WarehouseProduct.fetch(con,id);
			context.setValue("WarehouseProduct",obj);
			context.setNextUrl(path+"WarehouseProductView.jsp");
			return;
		}
		if(action_code.equalsIgnoreCase("deleteWarehouseProduct"))
		{
			context.setErrorUrl(path+"WarehouseProductList.jsp");
			deleteWarehouseProduct(con,context);
			backWarehouseProduct(con,context,path);
			return;
		}
			
		if(action_code.equalsIgnoreCase("toAddWarehouseProduct"))
		{
			WarehouseProduct obj = WarehouseProduct.fetch();
			WarehouseProductPageData data = (WarehouseProductPageData)PageData.fromTable(obj,WarehouseProductPageData.class);
			context.setValue("WarehouseProductPageData",data);
			context.setNextUrl(path+"WarehouseProductEdit.jsp");
			return;
		}
		if(action_code.equalsIgnoreCase("toUpdateWarehouseProduct"))
		{
			String id = context.getString("Id",null);
			WarehouseProduct obj = WarehouseProduct.fetch(con,id);
			WarehouseProductPageData data = (WarehouseProductPageData)PageData.fromTable(obj,WarehouseProductPageData.class);
			context.setValue("WarehouseProductPageData",data);
			context.setNextUrl(path+"WarehouseProductEdit.jsp");
			return;
		}
		if(action_code.equalsIgnoreCase("toCopyWarehouseProduct"))
		{
			String id = context.getString("Id",null);
			WarehouseProduct obj = WarehouseProduct.copy(WarehouseProduct.fetch(con,id));
			WarehouseProductPageData data = (WarehouseProductPageData)PageData.fromTable(obj,WarehouseProductPageData.class);
			context.setValue("WarehouseProductPageData",data);
			context.setNextUrl(path+"WarehouseProductEdit.jsp");
			return;
		}
		if(action_code.equalsIgnoreCase("saveWarehouseProduct"))
		{
			context.setErrorUrl(path+"WarehouseProductEdit.jsp");
			saveWarehouseProduct(con,context,loginContext);
			backWarehouseProduct(con,context,path);
			return;
		}
		if(action_code.equalsIgnoreCase("saveAndNewWarehouseProduct"))
		{
			context.setErrorUrl(path+"WarehouseProductEdit.jsp");
			saveWarehouseProduct(con,context,loginContext);
			WarehouseProduct obj = WarehouseProduct.fetch();
			WarehouseProductPageData data = (WarehouseProductPageData)PageData.fromTable(obj,WarehouseProductPageData.class);
			context.setValue("WarehouseProductPageData",data);
			context.setNextUrl(path+"WarehouseProductEdit.jsp");			
			return;
		}
		if(action_code.equalsIgnoreCase("backListWarehouseProduct"))
		{
			backWarehouseProduct(con,context,path);
			return;
		}
		
		//add by chengbiao.he start

		//add by chengbiao.he end

		Logger.throwException("WarehouseProductSrv","unknown action_code="+action_code);
	}
}
